# _*_ coding: UTF-8 _*_
# @Time     : 2021/11/1 11:37
# @Author   : yangshuaishuai
# @Site     : http://www.hzdledu.cn/
# @File     : w2s2_难道操作.py
# @Software : PyCharm

from selenium import webdriver
import time
from selenium.webdriver.support.select import Select  # 导入下拉菜单类


# 下拉菜单操作
# 有各种不同的下拉菜单，有js做的，还有标准html的<select>的定义
# js定义的下拉菜单，只能模拟用户的操作行为
# <select>定义的下拉菜单，除了可以通过模拟用户行为之外，也可以selenium的Select类来操作
def foo1():
    driver = webdriver.Chrome()
    driver.maximize_window()

    driver.get(r'D:\PYwordspace\hz_t2\web2\example.html')
    time.sleep(1)

    # 可以直接click下拉菜单的option
    driver.find_element_by_xpath('//*[@id="Selector"]/option[3]').click()
    time.sleep(2)

    # 通过select类操作下拉菜单
    element = driver.find_element_by_id('selector')  # 先找到<select>
    select = Select(element)  # 用element生成select类对象

    # 调用select类的方法
    select.select_by_visible_text('香蕉')  # 1.根据option的可见文本选择
    time.sleep(2)

    select.select_by_index(3)  # 2.根据option的index值，从0开始
    time.sleep(2)

    select.select_by_value('grape')  # 3.根据option的value属性
    time.sleep(2)

    driver.quit()


# 上传文件
# 1.上传按键是<input>:直接对<input>将文件路径sendkeys
# 2.上传按键不是<input>
def foo2():
    driver = webdriver.Chrome()
    driver.maximize_window()

    driver.get(r'D:\PYwordspace\hz_t2\web2\example.html')

    # 发送文件路径给<input>
    driver.find_element_by_name('attach[]').send_keys('D:\PYwordspace\hz_t2\web2\w2s1_元素的基本操作.py')
    time.sleep(2)

    driver.quit()


# 弹出框操作
def foo3():
    driver = webdriver.Chrome()
    driver.maximize_window()

    driver.get(r'D:\PYwordspace\hz_t2\web2\example.html')

    # 1.alert弹出框
    driver.find_element_by_name('alterbutton').click()
    time.sleep(2)
    # 切换并获取弹出框对象
    alert = driver.switch_to.alert
    # 调用对象的方法
    alert.accept()  # 点击确定
    time.sleep(2)
    # 2.confirm弹出框
    driver.find_element_by_name('confirmbutton').click()
    time.sleep(2)
    # 切换并获取弹出框对象
    confirm = driver.switch_to.alert
    confirm.dismiss()  # 点击取消
    time.sleep(2)
    confirm.accept()  # 点击确定
    time.sleep(2)

    # 3.prompt弹出框
    driver.find_element_by_name('promptbutton').click()
    # 切换并获取弹出框对象
    prompt = driver.switch_to.alert
    time.sleep(2)
    prompt.send_keys('汇智动力')  # 在弹出框输入
    prompt.accept()  # 点击确定
    time.sleep(2)
    prompt.accept()  # 点击确定
    time.sleep(2)

    driver.quit()


# 复选框checkbox操作
def foo4():
    driver = webdriver.Chrome()
    driver.maximize_window()

    driver.get(r'D:\PYwordspace\hz_t2\web2\example.html')

    # 直接点击复选框
    driver.find_element_by_id('web').click()
    time.sleep(2)
    # 获取勾选框的选中状态
    is_selected = driver.find_element_by_id('web').is_selected()
    print(is_selected)

    # 确保操作正确
    if is_selected:
        pass
    else:
        driver.find_element_by_id('web').click()

    driver.quit()


# 内嵌页面iframe操作
# 要操作iframe标签内的元素，必须先切换页面焦点到iframe标签
def foo5():
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.get('https://music.163.com/#/discover/toplist?id=3778678')
    time.sleep(3)

    # 切换到iframe
    # 切换方式3种：1.根据iframe的ID或name属性；2.根据iframe的index；3.根据iframe这个元素
    driver.switch_to.frame('g_iframe')  # 根据id
    driver.switch_to.frame()

    driver.find_element_by_xpath(
        "//*[@id='song-list-pre-cache']/div[1]/div[1]/table/tbody/tr[2]/td[2]/div[1]/div[1]/span").click()
    time.sleep(10)

    driver.quit()

    # 多窗口切换


def foo6():
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.get('http://news.baidu.com/')
    time.sleep(1)

    # 循环打开百度新闻：6个热点新闻
    for i in range(1, 7):
        driver.find_element_by_xpath(f'//*[@id="pane-news"]/div/ul/li[{i}]').click()
        time.sleep(1)

    # 获取浏览器的所有窗口句柄
    handles = driver.window_handles
    for handle in handles:
        driver.switch_to.window(handle)  # 切换窗口：根据handle句柄
        url = driver.current_url  # 获取网页url
        if url == 'http://cpc.people.com.cn/n1/2021/1031/c164113-32269457.html':
            break
        time.sleep(1)
    # driver.switch_to.window(handles[-1])#不建议使用handles的index值
    time.sleep(2)

    print(handles)

    driver.quit()


from selenium.webdriver.common.keys import Keys  # 导入特殊按键类


# 网页翻页
def foo7():
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.get('http://news.baidu.com/')
    time.sleep(1)
    for i in range(10):
        driver.find_element_by_xpath('/html').send_keys(Keys.PAGE_DOWN)  # keye.page_down:下翻页
        time.sleep(1)
    driver.find_element_by_xpath('/html').send_keys(Keys.HOME)  # Keys.HOME：回到页面顶部
    driver.quit(2)

    for i in range(10):
        driver.find_element_by_xpath('/html').send_keys(Keys.ARROW_DOWN)  # Keys.ARROW_DOWN:下箭头
        time.sleep(0.5)

    driver.quit()


if __name__ == '__main__':
    # foo1()
    # foo2()
    # foo3()
    # foo4()
    # foo5()
    # foo6()
    foo7()
